package cn.piaoyi.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Address;
import cn.piaoyi.bean.Cart;
import cn.piaoyi.bean.Category;
import cn.piaoyi.bean.City;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.bean.OrderGoods;
import cn.piaoyi.bean.OrderInfo;
import cn.piaoyi.bean.Payment;
import cn.piaoyi.bean.Price;
import cn.piaoyi.bean.Province;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.PiaoyiUtil;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AddressService;
import cn.piaoyi.service.CartService;
import cn.piaoyi.service.CityService;
import cn.piaoyi.service.OrderGoodsService;
import cn.piaoyi.service.OrderInfoService;
import cn.piaoyi.service.PaymentService;
import cn.piaoyi.service.PriceService;
import cn.piaoyi.web.interceptor.UserSessionAware;
import cn.piaoyi.web.servlet.UserSession;
import cn.piaoyi.web.ui.OptionsInt;
import cn.piaoyi.web.ui.OptionsLong;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class OrderSure extends BaseAction implements SessionAware,UserSessionAware{
	private static final Log logger = LogFactory.getLog(OrderSure.class);
	
	private CartService cartService;
	private OrderGoodsService orderGoodsService;
	private OrderInfoService orderInfoService;
	private PaymentService paymentService;
	private AddressService addressService;
	private CityService cityService;

	public CartService getCartService() {
		return cartService;
	}

	public void setCartService(CartService cartService) {
		this.cartService = cartService;
	}
	
	public OrderGoodsService getOrderGoodsService() {
		return orderGoodsService;
	}

	public void setOrderGoodsService(OrderGoodsService orderGoodsService) {
		this.orderGoodsService = orderGoodsService;
	}

	public OrderInfoService getOrderInfoService() {
		return orderInfoService;
	}

	public void setOrderInfoService(OrderInfoService orderInfoService) {
		this.orderInfoService = orderInfoService;
	}

	public PaymentService getPaymentService() {
		return paymentService;
	}

	public void setPaymentService(PaymentService paymentService) {
		this.paymentService = paymentService;
	}	
	public AddressService getAddressService() {
		return addressService;
	}

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

	public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}
	
	private long id;				
	private String consignee;	//收件人
	private int province = 0;	//省
	private int city = 0;		//市
	private String district;	//地区
	private String address;		//地址
	private String zipcode;		//邮编
	private String mobile;		//手机
	private String email;		//邮箱
	private int goodsAmount = 0 ;
	private int orderAmount = 0;
	private int shippingAmount = 0;
	
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getConsignee() {
		return consignee;
	}

	public void setConsignee(String consignee) {
		this.consignee = consignee;
	}
	
	public int getProvince() {
		return province;
	}

	public void setProvince(int province) {
		this.province = province;
	}

	public int getCity() {
		return city;
	}

	public void setCity(int city) {
		this.city = city;
	}

	public String getDistrict() {
		return district;
	}
	
	public void setDistrict(String district) {
		this.district = district;
	}
	
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	public int getGoodsAmount() {
		return goodsAmount;
	}

	public void setGoodsAmount(int goodsAmount) {
		this.goodsAmount = goodsAmount;
	}

	public int getOrderAmount() {
		return orderAmount;
	}

	public void setOrderAmount(int orderAmount) {
		this.orderAmount = orderAmount;
	}

	public int getShippingAmount() {
		return shippingAmount;
	}

	public void setShippingAmount(int shippingAmount) {
		this.shippingAmount = shippingAmount;
	}
	
	
	private List shippingList = new ArrayList();
	private List payList = new ArrayList();
	private List provinceList = new ArrayList();
	private List cityList = new ArrayList();
	private List citys;
	private List orderGoodses;
	private List carts;
	
	
	public List getProvinceList() {
		return provinceList;
	}

	public void setProvinceList(List provinceList) {
		this.provinceList = provinceList;
	}

	public List getCityList() {
		return cityList;
	}

	public void setCityList(List cityList) {
		this.cityList = cityList;
	}

	public List getShippingList() {
		return shippingList;
	}

	public void setShippingList(List shippingList) {
		this.shippingList = shippingList;
	}

	public List getPayList() {
		return payList;
	}

	public void setPayList(List payList) {
		this.payList = payList;
	}
	
	public List getCitys() {
		return citys;
	}

	public void setCitys(List citys) {
		this.citys = citys;
	}
	
	public List getOrderGoodses() {
		return orderGoodses;
	}

	public void setOrderGoodses(List orderGoodses) {
		this.orderGoodses = orderGoodses;
	}
	public List getCarts() {
		return carts;
	}

	public void setCarts(List carts) {
		this.carts = carts;
	}

	private void setShippingListValues(){
		//配送方式。1，已确认；2，己付款；
		shippingList.add(new OptionsInt(0, "自取"));
		shippingList.add(new OptionsInt(Constant.SHIPPING_AMOUNT, "快递"));
	}
	
	private void setPayListValues(){
		List l = this.getPaymentService().findPayments("id", 0);
		for(int i=0;i<l.size();i++){
			Payment p = (Payment) l.get(i);
			payList.add(new OptionsLong(p.getId(), p.getName()));
		}
	}
	
	private void setProvinceListValues(){
		List l = this.getCityService().findProvinceAll();
		for(int i=0;i<l.size();i++){
			Province p = (Province) l.get(i);
			provinceList.add(new OptionsLong(p.getId(), p.getName()));
		}
	}
	
	private void setCityListValues(long pid){
		Province p = this.getCityService().findProvinceById(pid);
		List l = this.getCityService().findCitysByProvinceId(p.getId());
		for(int i=0;i<l.size();i++){
			City c = (City) l.get(i);
			cityList.add(new OptionsLong(c.getId(), c.getName()));
		}
	}
	
	private void initValues(long pid){
		setShippingListValues();
		setPayListValues();
		setProvinceListValues();
		setCityListValues(pid);
	}
	
	
	private UserSession userSession;
	private Map session;

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}
	
	public String index(){
		this.setCarts(this.getCartService().findCarts(getUserSession().getUid(), "id", 0));
		if(this.carts == null || this.carts.isEmpty()){
			goodsAmount =0;
		}else{
			for(int i=0;i<this.carts.size();i++){
				Cart c = (Cart) carts.get(i);
				goodsAmount+=c.getTotalPrice();
			}
		}
		Address address = this.getAddressService().finAddressByUId(this.getUserSession().getUid());
		this.setConsignee(address.getConsignee());
		this.setProvince(address.getProvince());
		this.setCity(address.getCity());
		this.setDistrict(address.getDistrict());
		this.setAddress(address.getAddress());
		this.setZipcode(address.getZipcode());
		this.setMobile(address.getMobile());
		this.setEmail(address.getEmail());
		this.setAction("save");
		if(this.getProvince()==0){
			List pl = this.getCityService().findProvinceAll();
			Province p = (Province) pl.get(0);
			initValues(p.getId());
		}else{
			initValues(this.getProvince());
		}
		this.setCitys(this.getCityService().findCityAll());
		return SUCCESS;
	}

	/**
	 * 初始页
	 */
	public String doorder(){
		this.goodsAmount = 0;
		List l = this.getCartService().findCarts(getUserSession().getUid(), "id", 0);
		if(l == null || l.isEmpty()){
			
		}else{
			List orderGoodses = new ArrayList();
			for(int i=0;i<l.size();i++){
				Cart c = (Cart) l.get(i);
				OrderGoods og = new OrderGoods();
				og.setGoodsId(c.getGoodsId());
				og.setGoodsName(c.getGoods().getName());
				og.setDailyId(c.getDailyId());
				if(c.getDaily().getLimitTime()==0){
					og.setDailyName("不限时间");
				}else{
					og.setDailyName(PiaoyiUtil.formatDailyTime(c.getDaily()));
				}
				og.setPriceId(c.getPriceId());
				og.setPriceName(c.getPrice().getName());
				og.setPrice(c.getPrice().getPrice());
				og.setGoodsNumber(c.getGoodsNumber());
				og.setGoodsSite(c.getGoods().getSite());
				orderGoodses.add(og);
				this.goodsAmount+=og.getPrice()*og.getGoodsNumber();
			}
			Address address = this.getAddressService().finAddressByUId(this.getUserSession().getUid());
			Cart c = (Cart) l.get(0);
			OrderInfo oi = new OrderInfo();
			oi.setOrderNO(PiaoyiUtil.getUUID());
			oi.setOrderName(c.getGoods().getName());
			oi.setUserId(getUserSession().getUid());
			oi.setOrderStatus(0);
			oi.setShippingStatus(0);
			oi.setPayStatus(0);
			oi.setConsignee(address.getConsignee());
			Province province = this.getCityService().findProvinceById(this.getProvince());
			oi.setProvince(province.getName());
			City city = this.getCityService().findCityById(this.getCity());
			oi.setCity(city.getName());
			oi.setDistrict(address.getDistrict());
			oi.setAddress(address.getAddress());
			oi.setZipcode(this.zipcode);
			oi.setMobile(address.getMobile());
			oi.setEmail(address.getEmail());
			oi.setShippingId(this.shippingAmount);
			if(this.shippingAmount==0){
				oi.setShippingName("自取");
			}else{
				oi.setShippingName("快递");
			}
			oi.setPayId(0);
			oi.setPayName("");
			oi.setGoodsAmount(this.goodsAmount);
			oi.setShippingAmount(this.shippingAmount);
			oi.setOrderAmount(this.goodsAmount + this.shippingAmount);
			oi.setAddTime(new Date());
			oi.setConfirmTime(new Date());
			oi.setPayTime(new Date());
			oi.setShippingTime(new Date());
			oi.setShippingNO("");
			try {
				oi = this.getOrderInfoService().createOrderInfo(oi, orderGoodses);
				this.id = oi.getId();
			} catch (PiaoyiException e) {
				this.addActionError(this.getText("error.cart.updateerror"));
				return ERROR;
			}
			this.getCartService().removeCartByUid(getUserSession().getUid());
		}
		
		return "doorder";
	}

}
